package graduation.project.testcase.strategy;

/**
 *  According to the Dr Yu, the test selection strategy based on T-Model is that: select all test cases that MUMCUT generates;
 *  select points from OTP and RFP according to pair-wise method
 */

import graduation.project.body.OriginExpression;
import graduation.project.body.TestCase;
import graduation.project.body.TestCases;
import graduation.project.exception.NonExpressionException;

import java.util.ArrayList;
import java.util.LinkedHashSet;

public class T_ModelGenerator implements TestSetGenerator {

	public TestCases generateTestCases(String expression, int testCaseNumber) {
		
		try{
			OriginExpression originEx = new OriginExpression(expression);
			return generateTestCases(originEx, testCaseNumber);
		}catch(NonExpressionException e){
			System.out.println("the expression is not valid boolean expression");
			e.printStackTrace();
		}
		return null;
	}

	public TestCases generateTestCases(OriginExpression expression,
			int testCaseNumber) {

		TestCases result = new TestCases(expression.getExpression(), expression.getModel(), "T_Model");
		LinkedHashSet<TestCase> testSet = new LinkedHashSet<TestCase>();
//		System.out.println("-------------------------T-Model Bigin------------------------");
		MUMCUTGenerator mum = new MUMCUTGenerator();
		TestCases mumcut = mum.generateTestCases(expression, testCaseNumber);
				
		int and;
		int or;		
		int[] rfpSet = expression.getRfpSet();
		int[] otpSet = expression.getOtpSet();
		
		if(rfpSet != null){
			and = rfpSet[0];
			or = and;
			testSet.add(new TestCase(rfpSet[0], false));
			for(int i = 1; i < rfpSet.length; i++){
				if(and != (and & rfpSet[i]) || or != (or | rfpSet[i])){
					testSet.add(new TestCase(rfpSet[i], false));
					or = or | rfpSet[i];
					and = and & rfpSet[i];
				}
			}
		}
		if(otpSet != null){
			and = otpSet[0];
			or = and;
			testSet.add(new TestCase(otpSet[0], true));
			for(int i = 1; i < otpSet.length; i++){
				if(and != (and & otpSet[i]) || or != (or | otpSet[i])){
					testSet.add(new TestCase(otpSet[i], true));
					or = or | otpSet[i];
					and = and & otpSet[i];
				}
			}
		}
		
/*		System.out.println("MUMCUT.size(): " + mumcut.getTestSet().size());
		System.out.println("before add mumCut, testSet.size(): " + testSet.size());
		Iterator<TestCase> it = testSet.iterator();
		while(it.hasNext()){
			TestCase tc = it.next();
			System.out.println("test data: " + tc.getTestData() + ", expectant: " + tc.getExpectantResult());
		}*/
		
		testSet.addAll(mumcut.getTestSet());
		ArrayList<TestCase> set = new ArrayList<TestCase>();
		set.addAll(testSet);
		
/*		System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
		System.out.println("after add mumCut, testSet.size(): " + testSet.size());
		Iterator<TestCase> it2 = testSet.iterator();
		while(it2.hasNext()){
			TestCase tc = it2.next();
			System.out.println("test data: " + tc.getTestData() + ", expectant: " + tc.getExpectantResult());
		}
		System.out.println("-------------------------T-Model End------------------------");*/
		
		result.setTestSet(set);
		return result;
	}
	
	public static void main(String[] args){
		
		try {
			String expr = "!a * !b * !c * !d * !e * !f * !(d*e) * !(d*f) * !(e*f) + !g * !h * !i * !j * !k * !l * !(j*k) * !(j*l) * !(k*l) + !m * !n * !o * !p * !q * !r * !(p*q) * !(p*r) * !(q*r) + !s * !t * !u * !v * !w * !x * !(v*w) * !(v*x) * !(w*x) + !y * !z * !A * !B * !C * !D * !(B*C) * !(B*D) * !(C*D) + !E * !F * !G * !H * !I * !J";
			expr = expr.replaceAll("\\*", "").replaceAll("\\s", "");
			System.out.println("exp: " + expr);
			OriginExpression exp = new OriginExpression(expr);
			System.out.println("exp: " + expr);
			System.out.println("dnf: " + exp.getDNF());
			System.out.println("idnf: " + exp.getIDNF());
		} catch (NonExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
